package org.jeecg.modules.bigscreen.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.basic.demo.apartment.service.ILwgyBaseApartmentService;
import org.jeecg.modules.basic.demo.bed.entity.LwgyBaseBed;
import org.jeecg.modules.basic.demo.bed.mapper.LwgyBaseBedMapper;
import org.jeecg.modules.basic.demo.bed.service.ILwgyBaseBedService;
import org.jeecg.modules.basic.demo.school.service.ILwgyBaseSchoolService;
import org.jeecg.modules.basic.demo.student.service.ILwgyBaseStudentService;
import org.jeecg.modules.bigscreen.entity.*;
import org.jeecg.modules.bigscreen.mapper.JmReportMapper;
import org.jeecg.modules.sanitation2.service.ILwgyScoreSanitationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.Month;
import java.time.Year;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 報表數據
 * @Author: SN
 * @Date: 2021-05-18
 * @Version: V1.0
 */
@Service
@Slf4j
public class JmReportService {

    @Autowired
    private ILwgyBaseBedService iLwgyBaseBedService;

    @Autowired
    private ILwgyBaseStudentService iLwgyBaseStudentService;

    @Autowired
    private ILwgyBaseApartmentService iLwgyBaseApartmentService;


    @Autowired
    private ILwgyScoreSanitationService lwgyScoreSanitationService;

    @Autowired
    private ILwgyBaseSchoolService lwgyBaseSchoolService;

    @Autowired
    private JmReportMapper jmreportMapper;
    //获取住宿统计数据
    public List<JmReportHotelDate>  getHotelData(Integer pageNo,
                                                 Integer pageSize,
                                                 String campusName,
                                                 String buildingName) {
        long start = System.currentTimeMillis();
        Page<JmReportHotelDate> page = new Page<>(pageNo, pageSize);

        QueryWrapper<JmReportHotelDate> queryWrapper1 = new QueryWrapper<>();
        if(oConvertUtils.isNotEmpty(campusName)){
            queryWrapper1.like("ap5.name",campusName);
        }
        if(oConvertUtils.isNotEmpty(buildingName)){
            queryWrapper1.like("ap4.name", buildingName);
        }
        queryWrapper1.eq("ap.type", "room");
        queryWrapper1.eq("ap.del_flag", "0");
        queryWrapper1.groupBy("ap.pid");
        queryWrapper1.orderByAsc("ap5.name");
        queryWrapper1.orderByAsc("IF(LENGTH(ap4.NAME)=2,ap4.NAME,left(ap4.NAME,2))");
        queryWrapper1.orderByAsc("IF(LENGTH(ap4.NAME)=3,right(ap4.NAME,1),right(ap4.NAME,2))+0");
        queryWrapper1.orderByAsc("ap3.name+0");
        queryWrapper1.orderByAsc("ap2.name+0");
        List<JmReportHotelDate>  fjList = jmreportMapper.getRoomTotal(page,queryWrapper1);
        List<JmReportHotelDate>  bedList = new ArrayList<JmReportHotelDate>();
        List<String> lists = new ArrayList<>();
         fjList.stream().forEach(item->{
             lists.add(item.getChId());
        });
        if(lists.size()>0){
            bedList = jmreportMapper.getLayerBadTotal(lists);
        }

        if(fjList.size()>0){
            List<JmReportHotelDate> finalBedList = bedList;
            fjList.stream().forEach(item->{
                if( oConvertUtils.isNotEmpty(item.getCampusName())){
                    List<JmReportHotelDate>  listData =  finalBedList.stream().filter(item2->item.getChId().equals(item2.getChId())).collect(Collectors.toList());
                    if(listData.size()>0){
                        item.setAllBedCount(listData.get(0).getAllBedCount());
                        item.setOnBedCount(listData.get(0).getOnBedCount());
                        item.setUnBedCount(listData.get(0).getUnBedCount());
                        item.setBbCount(listData.get(0).getBbCount());//博士男
                        item.setBgCount(listData.get(0).getBgCount());//博士女
                        item.setYbCount(listData.get(0).getYbCount());//研究生男
                        item.setYgCount(listData.get(0).getYgCount());//研究生女
                        item.setBkbCount(listData.get(0).getBkbCount());//本科生男
                        item.setBkgCount(listData.get(0).getBkgCount());//本科生女
                        item.setZbCount(listData.get(0).getZbCount());//专科生男
                        item.setZgCount(listData.get(0).getZgCount());//专科生女
                        item.setBoy(listData.get(0).getBoy());
                        item.setGirl(listData.get(0).getGirl());
                        item.setProbability(String.valueOf(Integer.parseInt(listData.get(0).getOnBedCount())*100/Integer.parseInt(listData.get(0).getAllBedCount())));

                    }
                }


            });
        }
        long end = System.currentTimeMillis();
        log.info("studentList filter 耗时："+(end-start));

        return fjList;
    }

    //获取住宿统计数据
    public List<JmReportHotelDate>  getLhHotelData(Integer pageNo,
                                                 Integer pageSize,
                                                 String campusName,
                                                 String buildingName) {
        long start = System.currentTimeMillis();
        Page<JmReportHotelDate> page = new Page<>(pageNo, pageSize);

        QueryWrapper<JmReportHotelDate> queryWrapper1 = new QueryWrapper<>();
        if(oConvertUtils.isNotEmpty(campusName)){
            queryWrapper1.like("ap5.name",campusName);
        }
        if(oConvertUtils.isNotEmpty(buildingName)){
            queryWrapper1.like("ap4.name", buildingName);
        }
        queryWrapper1.eq("ap.type", "room");
        queryWrapper1.eq("ap.del_flag", "0");
        queryWrapper1.groupBy("ap3.pid");
        queryWrapper1.orderByAsc("ap5.name");
        queryWrapper1.orderByAsc("IF(LENGTH(ap4.NAME)=2,ap4.NAME,left(ap4.NAME,2))");
        queryWrapper1.orderByAsc("IF(LENGTH(ap4.NAME)=3,right(ap4.NAME,1),right(ap4.NAME,2))+0");
        List<JmReportHotelDate>  fjList = jmreportMapper.getRoomTotal(page,queryWrapper1);
        List<JmReportHotelDate>  bedList = new ArrayList<JmReportHotelDate>();
        List<String> lists = new ArrayList<>();
        fjList.stream().forEach(item->{
            lists.add(item.getLhId());
        });
        if(lists.size()>0){
            bedList = jmreportMapper.getBuildingBadTotal(lists);
        }

        if(fjList.size()>0){
            List<JmReportHotelDate> finalBedList = bedList;
            fjList.stream().forEach(item->{
                if( oConvertUtils.isNotEmpty(item.getCampusName())){
                    List<JmReportHotelDate>  listData =  finalBedList.stream().filter(item2->item.getLhId().equals(item2.getLhId())).collect(Collectors.toList());
                    if(listData.size()>0){
                        item.setAllBedCount(listData.get(0).getAllBedCount());
                        item.setOnBedCount(listData.get(0).getOnBedCount());
                        item.setUnBedCount(listData.get(0).getUnBedCount());
                        item.setBbCount(listData.get(0).getBbCount());//博士男
                        item.setBgCount(listData.get(0).getBgCount());//博士女
                        item.setYbCount(listData.get(0).getYbCount());//研究生男
                        item.setYgCount(listData.get(0).getYgCount());//研究生女
                        item.setBkbCount(listData.get(0).getBkbCount());//本科生男
                        item.setBkgCount(listData.get(0).getBkgCount());//本科生女
                        item.setZbCount(listData.get(0).getZbCount());//专科生男
                        item.setZgCount(listData.get(0).getZgCount());//专科生女
                        item.setBoy(listData.get(0).getBoy());
                        item.setGirl(listData.get(0).getGirl());
                        item.setProbability(String.valueOf(Integer.parseInt(listData.get(0).getOnBedCount())*100/Integer.parseInt(listData.get(0).getAllBedCount())));

                    }
                }


            });
        }
        long end = System.currentTimeMillis();
        log.info("studentList filter 耗时："+(end-start));
        log.info("listData size"+fjList.size());

        return fjList;
    }
    //获取住宿统计数据
    public List<JmReportHotelDate>  getXqHotelData(Integer pageNo,
                                                   Integer pageSize,
                                                   String campusName) {
        long start = System.currentTimeMillis();
        Page<JmReportHotelDate> page = new Page<>(pageNo, pageSize);

        QueryWrapper<JmReportHotelDate> queryWrapper1 = new QueryWrapper<>();
        if(oConvertUtils.isNotEmpty(campusName)){
            queryWrapper1.like("ap5.name",campusName);
        }
        queryWrapper1.eq("ap.type", "room");
        queryWrapper1.eq("ap.del_flag", "0");
        queryWrapper1.groupBy("ap4.pid");
        queryWrapper1.orderByAsc("ap5.name");

        List<JmReportHotelDate>  fjList = jmreportMapper.getRoomTotal(page,queryWrapper1);
        List<JmReportHotelDate>  bedList = new ArrayList<JmReportHotelDate>();
        List<String> lists = new ArrayList<>();
        fjList.stream().forEach(item->{
            lists.add(item.getXqId());
        });
        if(lists.size()>0){
            bedList = jmreportMapper.getCampusBadTotal(lists);
        }

        if(fjList.size()>0){
            List<JmReportHotelDate> finalBedList = bedList;
            fjList.stream().forEach(item->{
                if( oConvertUtils.isNotEmpty(item.getCampusName())){
                    List<JmReportHotelDate>  listData =  finalBedList.stream().filter(item2->item.getXqId().equals(item2.getXqId())).collect(Collectors.toList());
                    if(listData.size()>0){
                        item.setAllBedCount(listData.get(0).getAllBedCount());
                        item.setOnBedCount(listData.get(0).getOnBedCount());
                        item.setUnBedCount(listData.get(0).getUnBedCount());
                        item.setBbCount(listData.get(0).getBbCount());//博士男
                        item.setBgCount(listData.get(0).getBgCount());//博士女
                        item.setYbCount(listData.get(0).getYbCount());//研究生男
                        item.setYgCount(listData.get(0).getYgCount());//研究生女
                        item.setBkbCount(listData.get(0).getBkbCount());//本科生男
                        item.setBkgCount(listData.get(0).getBkgCount());//本科生女
                        item.setZbCount(listData.get(0).getZbCount());//专科生男
                        item.setZgCount(listData.get(0).getZgCount());//专科生女
                        item.setBoy(listData.get(0).getBoy());
                        item.setGirl(listData.get(0).getGirl());

                        item.setProbability(String.valueOf(Integer.parseInt(listData.get(0).getOnBedCount())*100/Integer.parseInt(listData.get(0).getAllBedCount())));
                    }
                }


            });
        }
        long end = System.currentTimeMillis();
        log.info("studentList filter 耗时："+(end-start));
        log.info("listData size"+fjList.size());

        return fjList;
    }
    //毕业生分布数据
    public List<JmReportGraduateDate>  getGraduateData(Integer pageNo,
                                                       Integer pageSize,
                                                       String campusName,
                                                       String buildingName) {
        long start = System.currentTimeMillis();
        Page<JmReportGraduateDate> page = new Page<>(pageNo, pageSize);
        QueryWrapper<JmReportGraduateDate> queryWrapper1 = new QueryWrapper<>();
        if(oConvertUtils.isNotEmpty(campusName)){
            queryWrapper1.eq("campus_name",campusName);
        }
        if(oConvertUtils.isNotEmpty(buildingName)){
            queryWrapper1.eq("building_name", buildingName);
        }
    //9月份前查询当前年毕业生（数据库大多是7.1为了保险起见算今年1月到9月）
    //9月份前查询明年毕业生（明年1月到9月）
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR); //2015，当前年份
        int month = calendar.get(Calendar.MONTH) + 1; //12，当前月，注意加 1
        String startTime ="";
        String endTime ="";
        if(month>9){
            //明年毕业生
            year = year+1;
            startTime = year+"-01-01 00:00:00";
            endTime = year+"-09-01 00:00:00";
        }else{
            //今年毕业生
            year = year;
            startTime = year+"-01-01 00:00:00";
            endTime = year+"-09-01 00:00:00";
        }

        //获取毕业生年份：9月份之前获取今年毕业生，九月份之后获取明年毕业生
        List<JmReportGraduateDate>  dataList = jmreportMapper.getGraduateTotal(page,queryWrapper1, startTime, endTime);

        List<JmReportGraduateDate>  dataList1=dataList.stream().filter(item->(
            Integer.parseInt(item.getFjGraduateNumber())>0
        )).collect(Collectors.toList());

        long end = System.currentTimeMillis();
        log.info("studentList filter 耗时："+(end-start));

        return dataList1;
    }


    //获取年纪学院住宿统计数据
    public List<JmReportFacultyDate>  getFacultyData(Integer pageNo,
                                                     Integer pageSize,
                                                     String campusName,
                                                     String yearName,
                                                     String facultyName   ) {
        Page<JmReportFacultyDate> page = new Page<>(pageNo, pageSize);
        QueryWrapper<JmReportFacultyDate> queryWrapper1 = new QueryWrapper<>();
        if (oConvertUtils.isNotEmpty(campusName)) {
            queryWrapper1.like("campusName", campusName);
        }
        if (oConvertUtils.isNotEmpty(yearName)) {
            queryWrapper1.like("yearName", yearName);
        }
        if (oConvertUtils.isNotEmpty(facultyName)) {
            queryWrapper1.like("facultyName", facultyName);
        }
        List<JmReportFacultyDate> xyList = jmreportMapper.getFacultyTotal(page, queryWrapper1);
/*        QueryWrapper<LwgyBaseBed> queryWrapper = new QueryWrapper<>();
        List<LwgyBaseBed> bedList = iLwgyBaseBedService.list(queryWrapper);
        List<JmReportFacultyDate> list =  new ArrayList<JmReportFacultyDate>();
        xyList.stream().forEach(item->{
            List<LwgyBaseBed> bedList2 =    bedList.stream().filter(item2->item.getStudentIds().contains(item2.getStudentId())).collect(Collectors.toList());
            System.out.println("bedList2:"+bedList2);
        });*/
        return xyList;
    }


    //获取年纪学院住宿统计数据
    public List<JmReportFacultyBuildingDate>  queryFacultyBuildingData(Integer pageNo,
                                                     Integer pageSize,
                                                     String facultyId,
                                                     String year  ) {
        Page<JmReportFacultyBuildingDate> page = new Page<>(pageNo, pageSize);
        QueryWrapper<JmReportFacultyBuildingDate> queryWrapper1 = new QueryWrapper<>();
        if (oConvertUtils.isNotEmpty(facultyId)) {
            queryWrapper1.eq("student_college", facultyId);
        }
        if (oConvertUtils.isNotEmpty(year)) {
            queryWrapper1.eq("student_year", year);
        }

            queryWrapper1.isNotNull("student_id");


        List<JmReportFacultyBuildingDate> bedList = jmreportMapper.getFacultyBuildingTotal(page,queryWrapper1);

        return bedList;
    }


    //获取年纪学院住宿统计数据

    public List<Map<String,String>>  queryStuLiveChart( String campusName,String buildingName  ) {
        List<Map<String,String>> bedList = jmreportMapper.queryStuLiveChart(campusName,buildingName);
        return bedList;
    }
    public List<Map<String,String>>  queryStuLiveLayerChart( String campusName,String buildingName  ) {
        List<Map<String,String>> bedList = jmreportMapper.queryStuLiveLayerChart(campusName,buildingName);
        return bedList;
    }

    public List<Map<String,String>>  queryRoomTypeChart( String campusName,String buildingName  ) {
        List<Map<String,String>> bedList = jmreportMapper.queryRoomTypeChart(campusName,buildingName);
        return bedList;
    }
    public List<Map<String,String>>  queryRoomTypeLayerChart( String campusName,String buildingName  ) {
        List<Map<String,String>> bedList = jmreportMapper.queryRoomTypeLayerChart(campusName,buildingName);
        return bedList;
    }

    public List<Map<String,String>>  queryCollegeChart( String campusName,String buildingName  ) {
        List<Map<String,String>> bedList = jmreportMapper.queryCollegeChart(campusName,buildingName);
        return bedList;
    }
    public List<Map<String,String>>  queryCollegeLayerChart( String campusName,String buildingName ,String unitName,String layerName  ) {
        List<Map<String,String>> bedList = jmreportMapper.queryCollegeLayerChart(campusName,buildingName,unitName,layerName);
        return bedList;
    }

    public List<Map<String,String>>  queryYearChart( String campusName,String buildingName  ) {
        List<Map<String,String>> bedList = jmreportMapper.queryYearChart(campusName,buildingName);
        return bedList;
    }
    public List<Map<String,String>>  queryYearLayerChart( String campusName,String buildingName  ,String unitName,String layerName ) {
        List<Map<String,String>> bedList = jmreportMapper.queryYearLayerChart(campusName,buildingName,unitName,layerName);
        return bedList;
    }

    public List<Map<String,String>>  queryNationChart( String campusName,String buildingName  ) {
        List<Map<String,String>> bedList = jmreportMapper.queryNationChart(campusName,buildingName);
        return bedList;
    }
    public List<Map<String,String>>  queryNationLayerChart( String campusName,String buildingName   ,String unitName,String layerName) {
        List<Map<String,String>> bedList = jmreportMapper.queryNationLayerChart(campusName,buildingName,unitName,layerName);
        return bedList;
    }

    // 空床位数据
    public List<JmReportRoomDate>  getNullRoomData(Integer pageNo,
                                                   Integer pageSize,
                                                   String campusName,
                                                   String buildingName) {
        long start = System.currentTimeMillis();
        Page<JmReportRoomDate> page = new Page<>(pageNo, pageSize);
        QueryWrapper<JmReportRoomDate> queryWrapper = new QueryWrapper<>();
        if(oConvertUtils.isNotEmpty(campusName)){
            queryWrapper.eq("campus_name",campusName);
        }
        if(oConvertUtils.isNotEmpty(buildingName)){
            queryWrapper.eq("building_name", buildingName);
        }

        queryWrapper.isNull("student_id").or().eq("student_id","");
        //获取毕业生年份：9月份之前获取今年毕业生，九月份之后获取明年毕业生
        List<JmReportRoomDate>  dataList = jmreportMapper.getNullRoomData(page,queryWrapper);

        long end = System.currentTimeMillis();
        log.info("studentList filter 耗时："+(end-start));

        return dataList;
    }
    public List<Map<String,String>>  queryCollegeRooms() {
        List<Map<String,String>> bedList = jmreportMapper.queryCollegeRooms();
        return bedList;
    }
}
